import os


def version(ver, os1):
    """
    os_version存放的是对应os不可用版本，os版本信息在info.conf中，发现新版本可添加到os_version字典中
    已知不可用版本：v7u9中6.0.10暂时不能使用
    已知可用版本：v7u9 and v10.0518中：5.0.4/5.0.8/6.2.6可用
    :return:
    """
    # global b
    os_version = {"v7u9": ["6.0.10"], "v10-0518": []}
    for i in os_version:
        if os1 == i:
            b = os_version[os1]
    for i in b:
        if ver == i:
                # print("这个os中这个镜像无法使用，建议更换其他版本")
        	exit("这个os中这个镜像无法使用，建议更换其他版本")
    return ver


def info():
    with open(os.path.abspath(os.path.dirname(os.path.dirname(__file__)))+"/info.conf", "r+", encoding="utf-8") as f:
        a = f.read()
        a = a.split("\n")
        name = a[0].split("=")[1]
        pkgversion = a[1].split("=")[1]
        author = a[2].split("=")[1]
        os1 = a[3].split("=")[1]
        pkgversion = version(pkgversion, os1)
        """
        name   and   author字段暂时未知可以有什么作用，暂时保留"""
        url = "https://download.redis.io/releases/redis-" + pkgversion + ".tar.gz"
        return url


class Myinstall:
    def __init__(self):
        self.url = info()
        self.pkg = self.url.split("/")[-1]
        self.Make = self.pkg.split(".tar.gz")[0]
        print(self.url, self.pkg, self.Make)
        self.install()
        self.check()

    def install(self):
        a = "wget " + self.url + """
tar -xvf """ + self.pkg + """
cd """ + self.Make + """
yum install gcc
make MALLOC=lib && make install
""" + "ln -s " + " src/redis-* /usr/bin/" + """
""" + "sed -i '/^daemonize*/d' " + " redis.conf" + """
""" + "sed -i '/^#\ daemonize*/d' " + " redis.conf" + """
""" + "sed -i 'N;4adaemonize\ yes' " + " redis.conf" + """
""" + "redis-server " + " redis.conf"
        with open("./install.sh", "w+") as f:
            f.write(a)
        os.system("chmod 777 install.sh")
        os.system("bash ./install.sh")

    def check(self):
        print("#############开始检查程序进程与端口##############")
        list1 = ["ss -nutlp|grep redis", "ps -ef |grep redis |egrep -v grep "]
        for i in list1:
            print(os.system(i).real)
            print("############################################")

if __name__ == '__main__':
    print(("installing......"))
    info()
    print(("安装完成"))
    Myinstall()
